package swing;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import carlyang.Out;

public class EventTest {

	public EventTest() {
		// TODO Auto-generated constructor stub
	}

	private static JFrame jFrame = null;
	private static JFrame jFrame2 = null;
	private static JFrame jFrame3 = null;
	private static JTextField jTextField = null;
	private static JTextField timeJTextField = null;
	private static JTextField xJTextField = null;
	private static JTextField yJTextField = null;
	private static SimpleDateFormat dateFormatAll = new SimpleDateFormat(
			"yyyy/MM/dd HH:mm:ss:SSS");
	private static SimpleDateFormat dateFormatTime = new SimpleDateFormat(
			"HH:mm:ss:SSS");

	/*
	 * 鼠标事件测试，使用UI显示事件类型，时间和坐标
	 */
	private static void MouseEventTest() {
		JFrame.setDefaultLookAndFeelDecorated(true);
		jFrame = new JFrame("MouseEvent Test");
		jFrame.setLayout(null);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 在Swing中可以不需这个设置要也能关闭
		jFrame.setBounds(20, 200, 500, 500);
		JLabel jLabel = new JLabel("Event:");
		JLabel jLabelX = new JLabel("X:");
		JLabel jLabelY = new JLabel("Y:");
		JLabel jLabelTime = new JLabel("Time:");
		jTextField = new JTextField();
		timeJTextField = new JTextField();
		xJTextField = new JTextField();
		yJTextField = new JTextField();

		jLabel.setBounds(20, 20, 60, 20);
		jTextField.setBounds(70, 20, 100, 20);
		jLabelTime.setBounds(20, 50, 50, 20);
		timeJTextField.setBounds(70, 50, 100, 20);
		jLabelX.setBounds(20, 80, 20, 20);
		xJTextField.setBounds(70, 80, 100, 20);
		jLabelY.setBounds(20, 110, 20, 20);
		yJTextField.setBounds(70, 110, 100, 20);

		jFrame.add(jLabel);
		jFrame.add(jTextField);
		jFrame.add(jLabelTime);
		jFrame.add(jLabelX);
		jFrame.add(jLabelY);
		jFrame.add(timeJTextField);
		jFrame.add(xJTextField);
		jFrame.add(yJTextField);

		jFrame.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				Out.println("mouseReleased:"
						+ dateFormatTime.format(new Date(e.getWhen())) + " X:"
						+ e.getX() + " Y:" + e.getY());
				jTextField.setText("mouseReleased");
				timeJTextField.setText(""
						+ dateFormatTime.format(new Date(e.getWhen())));
				xJTextField.setText("" + e.getX());
				yJTextField.setText("" + e.getY());
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				Out.println("mousePressed:");
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				Out.println("mouseExited:");
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				Out.println("mouseEntered:");
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				Out.println("mouseClicked:"
						+ dateFormatTime.format(new Date(e.getWhen())) + " X:"
						+ e.getX() + " Y:" + e.getY());
				jTextField.setText("mouseClicked");
				timeJTextField.setText(""
						+ dateFormatTime.format(new Date(e.getWhen())));
				xJTextField.setText("" + e.getX());
				yJTextField.setText("" + e.getY());
			}
		});
		// jFrame.setLocation(600, 300);
		jFrame.setVisible(true);

		Out.println("myFrame.getWidth=" + jFrame.getWidth());
		Out.println("constructGUI thread name:"
				+ Thread.currentThread().getName());
	}

	/*
	 * 对JRadioButton的ActionEvent的处理
	 */
	private static void RadioButtonEventTest() {
		JFrame.setDefaultLookAndFeelDecorated(true);
		jFrame2 = new JFrame("RadioButtonEvent Test");
		jFrame2.setLayout(new FlowLayout());
		jFrame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 在Swing中可以不需这个设置要也能关闭
		jFrame2.setBounds(540, 200, 500, 500);

		JRadioButton jRadioButton1 = new JRadioButton("Red");
		JRadioButton jRadioButton2 = new JRadioButton("Green");
		JRadioButton jRadioButton3 = new JRadioButton("Blue");
		jRadioButton1.setSize(20, 20);
		jRadioButton2.setSize(20, 20);
		jRadioButton3.setSize(20, 20);
		RadioClickListener listener = new RadioClickListener();// RadioClickListener作为内部类则方法需要非静态；因为静态方法使用时内部类还未加载
		jRadioButton1.addActionListener(new RadioClickListener());
		jRadioButton2.addActionListener(new RadioClickListener());
		jRadioButton3.addActionListener(new RadioClickListener());
		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(jRadioButton1);
		buttonGroup.add(jRadioButton2);
		buttonGroup.add(jRadioButton3);
		jRadioButton1.setSelected(true);

		jFrame2.add(jRadioButton1);
		jFrame2.add(jRadioButton2);
		jFrame2.add(jRadioButton3);

		// jFrame.setLocation(600, 300);
		jFrame2.setVisible(true);

		Out.println("myFrame.getWidth=" + jFrame.getWidth());
		Out.println("constructGUI thread name:"
				+ Thread.currentThread().getName());
	}

	/*
	 * 对键盘事件KeyEvent的处理
	 */
//	private static JTextField jtextFieldKey = null;
	private static JLabel jLabelKey = null;
	private static void KeyEventTest() {
		JFrame.setDefaultLookAndFeelDecorated(true);
		jFrame3 = new JFrame("KeyEvent Test");
		jFrame3.setLayout(null);
		jFrame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 在Swing中可以不需这个设置要也能关闭
		jFrame3.setBounds(1060, 200, 500, 500);

		final JTextField jtextFieldKey = new JTextField();
		jtextFieldKey.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				Out.println("keyTyped:"+e.getKeyChar());
				e.setKeyChar(Character.toUpperCase((e.getKeyChar())));
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				Out.println("keyReleased:");
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				Out.println("keyPressed:");
			}
		});
		jtextFieldKey.setBounds(20, 20, 300, 20);
		jLabelKey = new JLabel("Show Input:");
		jLabelKey.setBounds(20, 80, 300, 20);
		jLabelKey.setForeground(Color.GRAY);
		JButton jButton = new JButton("Get Input");
		jButton.setBounds(20, 50, 100, 20);
		jButton.setBackground(Color.BLACK);
		jButton.setForeground(Color.white);
		jButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jLabelKey.setText(jtextFieldKey.getText());
			}
		});
		jFrame3.add(jtextFieldKey);
		jFrame3.add(jButton);
		jFrame3.add(jLabelKey);
		// jFrame.setLocation(600, 300);
		jFrame3.setVisible(true);

		Out.println("myFrame.getWidth=" + jFrame.getWidth());
		Out.println("constructGUI thread name:"
				+ Thread.currentThread().getName());
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				// constructGUI();// 使用static方法或创建对象
				MouseEventTest();
				RadioButtonEventTest();
				KeyEventTest();
			}
		});
	}

}

class MouseClickListener extends MouseAdapter {

}

class RadioClickListener implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent e) {
		String command = "Command:" + e.getActionCommand() + ",ID:" + e.getID()
				+ ",Modifiers:" + e.getModifiers() + ",paramString:"+ e.paramString();
				// + "Source:" + e.getSource() + " toString:" + e.toString();
		Out.println(command);
	}
}